﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Log
{
    public static class LogFile
    {
        private static string _sLogFileName = "";

        public static string GetLogFileName()
        {
            if (_sLogFileName == "")
            {
                string path = "";
                path = Application.StartupPath + @"\log";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                _sLogFileName = path + @"\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                FileStream stream = null;
                StreamWriter writer = null;
                stream = File.Open(_sLogFileName, FileMode.Append);
                writer = new StreamWriter(stream);
                writer.Close();
                stream.Close();
                writer.Dispose();
                stream.Dispose();
            }
            return _sLogFileName;
        }

        public static void WriteLog(string logContent)
        {
            if (_sLogFileName == "")
            {
                string path = "";
                path = Application.StartupPath + @"\log";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                _sLogFileName = path + @"\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            }
            FileStream stream = null;
            StreamWriter writer = null;
            stream = File.Open(_sLogFileName, FileMode.Append);
            writer = new StreamWriter(stream, Encoding.Default);
            writer.WriteLine(logContent);
            writer.Close();
            stream.Close();
            writer.Dispose();
            stream.Dispose();
        }
    }
}

